﻿Imports System.Text.RegularExpressions
Imports FL_8x0.Common
Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.CommandWpf

Namespace FL_8x0.Viewmodel

    Public Class PasswordViewModel
        Inherits ViewModelBase

        Private _oldPass As String
        Private _newPass As String
        Private _confirm As String

        Private _applyCommand As RelayCommand(Of Window)
        Private _cancelCommand As RelayCommand(Of Window)

        Public Property OldPass As String
            Get
                Return _oldPass
            End Get
            Set(value As String)
                _oldPass = value
            End Set
        End Property

        Public Property NewPass As String
            Get
                Return _newPass
            End Get
            Set(value As String)
                _newPass = value
            End Set
        End Property

        Public Property Confirm As String
            Get
                Return _confirm
            End Get
            Set(value As String)
                _confirm = value
            End Set
        End Property

        Public Property ApplyCommand As RelayCommand(Of Window)
            Get
                Return _applyCommand
            End Get
            Set(value As RelayCommand(Of Window))
                _applyCommand = value
            End Set
        End Property

        Public Property CancelCommand As RelayCommand(Of Window)
            Get
                Return _cancelCommand
            End Get
            Set(value As RelayCommand(Of Window))
                _cancelCommand = value
            End Set
        End Property

        Public Sub New()
            ApplyCommand = New RelayCommand(Of Window)(AddressOf Apply)
            CancelCommand = New RelayCommand(Of Window)(AddressOf Cancel)
        End Sub

        Private Function CheckInput() As Boolean
            '密码
            If _oldPass = "" Then
                log.WriteLogError("输入参数检查", "[旧密码]不能为空！")
                Return False
            End If

            If _newPass.Length < 8 Then
                log.WriteLogError("输入参数检查", "[新密码]长度不足8个字符")
                Return False
            End If

            If _newPass.Length > 250 Then
                log.WriteLogError("输入参数检查", "[新密码]长度超过250个字符")
                Return False
            End If

            If _oldPass = _newPass Then
                log.WriteLogError("输入参数检查", "[旧密码]和[新密码]相同！")
                Return False
            End If

            If _newPass <> _confirm Then
                log.WriteLogError("输入参数检查", "[新密码]和[新密码确认]不同！")
                Return False
            End If

            Dim rg1 As Regex = New Regex("[A-Z]")
            Dim rg2 As Regex = New Regex("[a-z]")
            Dim rg3 As Regex = New Regex("[0-9]")
            Dim rg4 As Regex = New Regex("[_\~\`\!\@\#\$\%\^\&\*\(\)\+\-\{\}\[\]\\\|\;\:\'\,\.\/\<\>\?]")

            Dim ck1 As Boolean = rg1.IsMatch(_newPass)
            Dim ck2 As Boolean = rg2.IsMatch(_newPass)
            Dim ck3 As Boolean = rg3.IsMatch(_newPass)
            Dim ck4 As Boolean = rg4.IsMatch(_newPass)

            If Not ((ck1 And ck2 And ck3) Or (ck1 And ck2 And ck4) Or (ck1 And ck3 And ck4) Or (ck2 And ck3 And ck4)) Then
                log.WriteLogError("输入参数检查", "[新密码]至少包含[数字]、[大写字母]、[小写字母]、[特殊字符]中的三种。")
                Return False
            End If

            If CheckUserSimple(SystemData.g_strUserName, _oldPass) <> True Then
                log.WriteLogError("输入参数检查", "[旧密码]错误！")
                Return False
            End If

            Dim strOldPassword As String = ReadOldPassword(SystemData.g_strUserName)

            If strOldPassword <> "" Then '[前3次密码]有记录
                If InStr(strOldPassword, _newPass) > 0 Then '新密码重复了
                    log.WriteLogError("输入参数检查", "[新密码]已被使用过！")
                    Return False
                End If
            End If





            Return True

        End Function

        Private Sub Apply(window As Window)
            If CheckInput() Then
                EditUserPassword(SystemData.g_strUserName, _newPass)
                window.DialogResult = True
            End If
        End Sub

        Private Sub Cancel(window As Window)
            window.DialogResult = False
        End Sub

    End Class
End Namespace